﻿namespace UnityEditorInternal
{
    using System;
    using System.IO;
    using UnityEditor;
    using UnityEngine;

    public class MacroWindow : EditorWindow
    {
        private string m_LastMacroLogForKeyboardControl = string.Empty;
        private string m_LastMacroLogRepaint = string.Empty;
        private Vector2 m_Position;
        private Vector2 m_PositionExecute;
        private string m_Text = string.Empty;

        public static void LoadDefaultRegressionLayout()
        {
            string path = Path.Combine(Path.Combine(EditorApplication.applicationContentsPath, "Resources"), "RegressionRig.wlt");
            WindowLayout.CloseWindows();
            WindowLayout.LoadWindowLayout(path);
        }

        private void OnGUI()
        {
            GUILayout.BeginHorizontal(new GUILayoutOption[0]);
            Macros.enabled = GUILayout.Toggle(Macros.enabled, "Enabled", new GUILayoutOption[0]);
            Macros.recordEvents = GUILayout.Toggle(Macros.recordEvents, "Record Revents", new GUILayoutOption[0]);
            if (GUILayout.Button("Clear", new GUILayoutOption[0]))
            {
                Macros.ClearLog();
            }
            if (GUILayout.Button("Load Layout", new GUILayoutOption[0]))
            {
                LoadDefaultRegressionLayout();
                ShowMacroWindow();
                GUIUtility.ExitGUI();
            }
            GUILayout.EndHorizontal();
            if ((GUI.GetNameOfFocusedControl() == "Macros") && (this.m_LastMacroLogForKeyboardControl != Macros.log))
            {
                GUIUtility.keyboardControl = 0;
            }
            this.m_LastMacroLogForKeyboardControl = Macros.log;
            GUILayoutOption[] options = new GUILayoutOption[] { GUILayout.MinHeight((float) ((Screen.height / 2) - 0x2d)) };
            this.m_Position = EditorGUILayout.BeginScrollView(this.m_Position, options);
            GUI.SetNextControlName("Macros");
            GUILayoutOption[] optionArray2 = new GUILayoutOption[] { GUILayout.MinHeight(1000f) };
            EditorGUILayout.TextArea(Macros.log, optionArray2);
            EditorGUILayout.EndScrollView();
            if (GUILayout.Button("Execute Below", new GUILayoutOption[0]))
            {
                Macros.ExecuteMacro(this.m_Text);
                GUIUtility.ExitGUI();
            }
            GUILayoutOption[] optionArray3 = new GUILayoutOption[] { GUILayout.MinHeight((float) ((Screen.height / 2) - 0x2d)) };
            this.m_PositionExecute = EditorGUILayout.BeginScrollView(this.m_PositionExecute, optionArray3);
            GUILayoutOption[] optionArray4 = new GUILayoutOption[] { GUILayout.MinHeight(1000f) };
            this.m_Text = EditorGUILayout.TextArea(this.m_Text, optionArray4);
            EditorGUILayout.EndScrollView();
        }

        private void OnInspectorUpdate()
        {
            if (this.m_LastMacroLogRepaint != Macros.log)
            {
                this.m_LastMacroLogRepaint = Macros.log;
                base.Repaint();
            }
        }

        private static void ShowMacroWindow()
        {
            EditorWindow.GetWindow(typeof(MacroWindow));
        }
    }
}

